package com.tau.iplayer.client.models;

import java.io.Serializable;

import com.google.gwt.core.client.GWT;
import com.google.gwt.gadgets.client.gwtrpc.GadgetsGwtRpc;
import com.google.gwt.gadgets.client.osapi.people.Person;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.tau.iplayer.client.DataStoreService;
import com.tau.iplayer.client.DataStoreServiceAsync;

public class Track implements Serializable, Comparable<Track> {

	private Long id;
	private String title;
	private String artist;
	private int duration;
	private String videoID;
	private String thumbnailURL;
	private double rating;

	public Track() {
		this(null, null, 0, null, null, 0);
	}

	public Track(String title, String artist, int duration, String videoID,
			String thumbnailURL, double rating) {
		this.title = title;
		this.artist = artist;
		this.duration = duration;
		this.videoID = videoID;
		this.thumbnailURL = thumbnailURL;
		this.rating = rating;
	}

	public Track(String title, int duration, String videoID, String thumbnailURL) {
		this(title, "", duration, videoID, thumbnailURL, 0);
	}

	public Track(Long id, String title, String artist, int duration,
			String videoID, String thumbnailURL, double rating) {
		this(title, artist, duration, videoID, thumbnailURL, rating);
		this.id = id;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @return the artist
	 */
	public String getArtist() {
		return artist;
	}

	/**
	 * @return the duration
	 */
	public int getDuration() {
		return duration;
	}

	/**
	 * @return the id
	 */
	public String getVideoID() {
		return videoID;
	}

	/**
	 * @return the thumbnailURL
	 */
	public String getThumbnailURL() {
		return thumbnailURL;
	}

	/**
	 * @return the rating
	 */
	public double getRating() {
		return rating;
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @param artist
	 *            the artist to set
	 */
	public void setArtist(String artist) {
		this.artist = artist;
	}

	/**
	 * @param rating
	 *            the rating to set
	 */
	public void setRating(double rating) {
		this.rating = rating;
	}

	@Override
	public String toString() {
		String artist = getArtist();
		String title = getTitle();
		String separator = (artist.length() == 0 || title.length() == 0) ? ""
				: " - ";
		return artist + separator + title;
	}

	@Override
	public int compareTo(Track track) {
		return this.toString().compareToIgnoreCase(track.toString());
	}

	/**
	 * updates a track
	 * 
	 * @param newTrack
	 *            the updated track
	 * @param reaload
	 *            true if playlist table needs to be realoaded
	 */
	public void updateTrack(Person viewer, AsyncCallback<Void> callback) {
		DataStoreServiceAsync dataStoreService = GWT
				.create(DataStoreService.class);
		GadgetsGwtRpc.redirectThroughProxy((ServiceDefTarget) dataStoreService);
		dataStoreService.updateTrack(viewer.getId(), this, callback);
	}

	@Override
	public boolean equals(Object obj) {
		Track track = (Track) obj;
		if (this.id != null && track.id != null) {
			return (this.id.equals(track.id));
		}
		return false;
	}

}
